<template>
  <view class="goods_details" v-if="goods_info.goods_name != ''">
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item, index) in goods_info.pics" :key="index">
        <image :src="item.pics_big" @click="preview(index)"></image>
      </swiper-item>
    </swiper>
    
    <!-- 商品信息 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">
        ￥{{ goods_info.goods_price }}
      </view>
      <!-- 商品信息中的body -->
      <view class="goods-info-body">
        <!-- 商品标题 -->
        <view class="goods-name">
          {{ goods_info.goods_name }}
        </view>
        <!-- 是否收藏 -->
        <view class="favor">
          <uni-icons type="star" size="18"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费信息 -->
      <view class="yf">
        快递: <text class="yf-condition">免运费</text>
      </view>
    </view>
    <!-- 商品介绍 -->
    <!-- 渲染节点组件 -->
    <view class="goods-introduce">
      <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    </view>
    
    <!-- 商品导航组件区域 -->
    <view class="goods-nav">
      <uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="onClick" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import { mapState, mapMutations, mapGetters } from 'vuex'
  export default {
    computed: {
      // 展开后是这样的 cart: ƒ mappedState()
      // ...mapState('m_cart', ['cart']),
      
      // 如果 [] 不指定哪个state的属性的话，那么就是全部的意思
      ...mapState('m_cart', []),
      ...mapGetters('m_cart', ['total'])
    },
    watch: {
      // total(newVal) {
      //   let optionResult = this.options.find(option => option.text === '购物车')
        
      //   if(optionResult) {
      //     optionResult.info = newVal
      //   }
      // },
      total: {
        handler(newVal) {
          let optionResult = this.options.find(option => option.text === '购物车')
          
          if(optionResult) {
            optionResult.info = newVal
          }
        },
        // 首次加载就触发一次
        immediate: true
      }
    },
    data() {
      return {
        goods_info: {},
        options: [{
          icon: 'shop',
          text: '店铺'
        }, {
          icon: 'cart',
          text: '购物车',
          info: this.total
        }], 
        buttonGroup: [{
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
        }, {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }]
      };
    },
    onLoad(options) {
      this.getGoodsInfo(options.goods_id)
    },
    methods: {
      // 挂载 store 中 m_cart 的方法到该组件中
      ...mapMutations('m_cart', ['addGoodsToCart']),
      async getGoodsInfo(goods_id) {
        let {data: res} = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        // console.log(res)
        if(res.meta.status !== 200) return uni.$showMsg()
        
        // 修改img标签的小白标问题，还有图片的后缀问题, 记得需要重新赋值，因为replace替换完后返回的是一个新的值, 不是在原来的字符串进行替换的
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display: block;"').replace(/.webp/g, '.jpg')
        
        this.goods_info = res.message
        // console.log(this.goods_info)
      },
      preview(index) {
        uni.previewImage({
          // 当前渲染第几个索引的图片
          current: index,
          // map生成一个新数组，里面的值是循环遍历pic中的每一项，每一项经过回调函数处理返回后得到的一个新数组
          urls: this.goods_info.pics.map(picStr => picStr.pics_big)
        })
      },
      onClick(e) {
        // console.log(e)
        if(e.content.text == '购物车') {
          // 跳转到 购物车 的tab页面
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
        if(e.content.text == '店铺') {
          console.log('跳转到店铺')
        }
      },
      buttonClick(e) {
        if(e.content.text == '加入购物车') {
          let goods = {
            // 包含一下属性： { goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
            goods_id: this.goods_info.goods_id,
            goods_name: this.goods_info.goods_name,
            goods_price: this.goods_info.goods_price,
            goods_count: 1,
            goods_small_logo: this.goods_info.goods_small_logo,
            goods_state: true
          }
          this.addGoodsToCart(goods)
        }
      }
    }
  }
</script>

<style lang="scss">
  .goods_details {
    padding-bottom: 50px;
    background-color: #FFFFFF;
  }
  swiper {
    height: 650rpx;
    
    image {
      width: 100%;
      height: 100%;
    }
  }
  
  .goods-info-box {
    padding: 10px 0 10px 10px;
    .price {
      margin: 10px 0;
      font-size: 18px;
      color: #C00000;
    }
    .goods-info-body {
      display: flex;
      justify-content: space-between;
      .goods-name {
        margin-right: 10px;
        font-size: 14px;
      }
      .favor {
        width: 100px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
        font-size: 12px;
      }
    }
  }
    
  .yf {
    margin: 10px 0;
    font-size: 12px;
    color: gray;
    
    &>text {
      font-size: 13px;
      margin-left: 3px;
    }
  }
  .goods-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 99999;
  }
</style>
